package com.llmcu.msb.architect.concurrent.basic;

import java.util.concurrent.TimeUnit;

/**
 * wait被打断后，会重置打断标志位
 *
 * @author liuling
 * @date 2024/9/17 18:59
 */
public class InterruptAndWait {
    private static Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            synchronized (object) {
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    System.out.println("线程被打断");
                    // 根据wait()方法注释，抛出异常后会重置打断标志位，所以打印为false
                    System.out.println(Thread.currentThread().isInterrupted());
                }
            }
        });

        thread.start();
        TimeUnit.SECONDS.sleep(2);
        thread.interrupt();
    }
}
